part of my_base_lib;

class SpUtils {
  static SharedPreferences? sp;

  static init() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    sp = prefs;
  }

  static String toPrivateKey(String key) {
    return CommonUtils.getMd5String("${UserUtils.getUsername()}_$key");
  }

  ///String
  static _saveString(String key, String value) {
    sp?.setString(key, value ?? "");
  }

  static String? _getString(String key) {
    Object? value = sp?.get(key);
    if (value == null) {
      return null;
    }
    return value as String;
  }

  static savePublicString(String key, String value) {
    _saveString(key, value ?? "");
  }

  static getPublicString(String key) {
    return _getString(key);
  }

  static savePrivateString(String key, String? value) {
    _saveString(toPrivateKey(key), value ?? "");
  }

  static getPrivateString(String key) {
    return _getString(toPrivateKey(key));
  }

  ///StringList
  static _saveStringList(String key, List<String> value) {
    sp?.setStringList(key, value);
  }

  static List<String>? _getStringList(String key) {
    Object? value = sp?.get(key);
    if (value == null) {
      return null;
    }
    try {
      return value as List<String>;
    } catch (e) {
      return null;
    }
  }

  static savePublicStringList(String key, List<String> value) {
    _saveStringList(key, value);
  }

  static List<String>? getPublicStringList(String key) {
    return _getStringList(key);
  }

  static savePrivateStringList(String key, List<String> value) {
    _saveStringList(toPrivateKey(key), value);
  }

  static List<String>? getPrivateStringList(String key) {
    return _getStringList(toPrivateKey(key));
  }

  ///bool
  static _saveBool(String key, bool value) {
    sp?.setBool(key, value);
  }

  static bool _getBool(String key, bool defaultValue) {
    Object? value = sp?.get(key);
    if (value == null) {
      return defaultValue;
    }
    return value as bool;
  }

  static savePublicBool(String key, bool value) {
    _saveBool(key, value);
  }

  static bool getPublicBool(String key, bool defaultValue) {
    return _getBool(key, defaultValue);
  }

  static savePrivateBool(String key, bool value) {
    _saveBool(toPrivateKey(key), value);
  }

  static bool getPrivateBool(String key, bool defaultValue) {
    return _getBool(toPrivateKey(key), defaultValue);
  }

  ///int
  static _saveInt(String key, int value) {
    sp?.setInt(key, value);
  }

  static int _getInt(String key, int defaultValue) {
    Object? value = sp?.get(key);
    if (value == null) {
      return defaultValue;
    }
    return value as int;
  }

  static savePublicInt(String key, int value) {
    _saveInt(key, value);
  }

  static int getPublicInt(String key, int defaultValue) {
    return _getInt(key, defaultValue);
  }

  static savePrivateInt(String key, int value) {
    _saveInt(toPrivateKey(key), value);
  }

  static int getPrivateInt(String key, int defaultValue) {
    return _getInt(toPrivateKey(key), defaultValue);
  }

  ///double
  static _saveDouble(String key, double value) {
    sp?.setDouble(key, value);
  }

  static double _getDouble(String key, double defaultValue) {
    Object? value = sp?.get(key);
    if (value == null) {
      return defaultValue;
    }
    return value as double;
  }

  static savePublicDouble(String key, double value) {
    _saveDouble(key, value);
  }

  static double getPublicDouble(String key, double defaultValue) {
    return _getDouble(key, defaultValue);
  }

  static savePrivateDouble(String key, double value) {
    _saveDouble(toPrivateKey(key), value);
  }

  static double getPrivateDouble(String key, double defaultValue) {
    return _getDouble(toPrivateKey(key), defaultValue);
  }
}
